package eu.best.sercheng.examples;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import eu.best.sercheng.model.entity.Keyword;
import eu.best.sercheng.model.entity.RelationKeyPage;
import eu.best.sercheng.model.entity.RelationKeyPagePK;
import eu.best.sercheng.model.entity.VisitedPage;

public class TryDatabase {
	private static SessionFactory factory;
	
	static {
		AnnotationConfiguration config = new AnnotationConfiguration();
		config.addAnnotatedClass(Keyword.class);
		config.addAnnotatedClass(VisitedPage.class);
		config.addAnnotatedClass(RelationKeyPage.class);
		config.configure("hibernate.cfg.xml");
		new SchemaExport(config).create(true, true);
		factory = config.buildSessionFactory();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println();
//		AnnotationConfiguration config = new AnnotationConfiguration();
//		config.addAnnotatedClass(Keyword.class);
//		config.addAnnotatedClass(VisitedPage.class);
//		config.addAnnotatedClass(RelationKeyPage.class);
//		config.configure("hibernate.cfg.xml");
//		
////		new SchemaExport(config).create(true, true);
//		
//		SessionFactory factory = config.buildSessionFactory();
//		Session session = factory.getCurrentSession();
		Session session = factory.getCurrentSession();

		
		
		session.beginTransaction();
		
		
		Keyword keyword = new Keyword();
		keyword.setKeyword("hi");
		Keyword keyword2 = new Keyword();
		keyword2.setKeyword("hello");
		
		
		VisitedPage visitedPages = new VisitedPage();
		visitedPages.setURL("www.sdsad");
		
		RelationKeyPage relationKeyPage = new RelationKeyPage();
		RelationKeyPagePK relationKeyPagePK = new RelationKeyPagePK();
		relationKeyPagePK.setKeyword(keyword);
		relationKeyPagePK.setVisitedPage(visitedPages);
		relationKeyPage.setPk(relationKeyPagePK);
		
		
		//		relationKeyPage.setKeyword(keyword);
//		relationKeyPage.setVisitedPage(visitedPages);
		
		
//		RelationKeyPage relationKeyPage2 = new RelationKeyPage();
//		relationKeyPage2.setKeyword(keyword2);
//		relationKeyPage2.setVisitedPage(visitedPages);
		
		
//		session.saveOrUpdate(keyword2);
		session.saveOrUpdate(visitedPages);
		session.saveOrUpdate(keyword);
		session.saveOrUpdate(relationKeyPage);
//		session.saveOrUpdate(relationKeyPage2);
		session.getTransaction().commit();


	}

}
